// schema扩展相关文档请参阅：https://uniapp.dcloud.net.cn/uniCloud/jql-schema-ext.html
module.exports = {
  trigger: {
    // 新增前触发
    beforeCreate: async function({
      collection,
      operation,
      addData,
      addDataList,
      clientInfo,
      userInfo
    } = {}) {
      for (let i = 0; i < addDataList.length; i++) {
        const addDataItem = addDataList[i]
        addDataItem.id = Date.now().toString(36) + Math.random().toString(36).substring(2);
        addDataItem.created = Date.now();
        addDataItem.updated = Date.now();
      }
    },
    // 更新后触发
    afterUpdate: async function({
      collection,
      operation,
      updateData,
      addDataList,
      clientInfo
    } = {}) {
      updateData.updated = Date.now() // 更新数据的update_date字段赋值为当前服务器时间
    }
  }
}